package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.Dto.DishDTO;
import org.example.entity.Category;
import org.example.entity.Dish;
import org.example.entity.DishFlavor;
import org.example.mapper.DishMapper;
import org.example.service.CategoryService;
import org.example.service.DishFlavorService;
import org.example.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {


    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;


    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        //保存菜品信息
        this.save(dishDTO);
        Long dishId = dishDTO.getId();
        //保存菜品口味信息
        dishDTO.getFlavors()
                .forEach(flavor -> flavor.setDishId(dishId));
        this.dishFlavorService.saveOrUpdateBatch(dishDTO.getFlavors());
    }

    @Override
    public List<DishDTO> listWithFlavor(List<Dish> records) {
        return records.stream().map(record -> {
            DishDTO dishDTO = new DishDTO();
            BeanUtils.copyProperties(record, dishDTO);
            Long categotyId = record.getCategoryId();
            Category category = categoryService.getById(categotyId);
            if(category != null){
                String categoryName = category.getName();
                dishDTO.setCategoryName(categoryName);
            }
            return dishDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public DishDTO getByIdWithFlavor(Long id) {
        //查询菜品信息
        Dish dish = this.getById(id);
        //查询菜品口味信息
        DishDTO dishDTO = new DishDTO();
        //拷贝
        BeanUtils.copyProperties(dish, dishDTO);
        //查询菜品分类信息
        Long categotyId = dish.getCategoryId();
        Category category = categoryService.getById(categotyId);
        //如果分类信息不为空
        if(category != null){
            //获取分类名称
            String categoryName = category.getName();
            //设置分类名称
            dishDTO.setCategoryName(categoryName);
        }
        List<DishFlavor> dishFlavors = dishFlavorService.list(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, id));
        dishDTO.setFlavors(dishFlavors);
        return dishDTO;
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     */
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        this.updateById(dishDTO);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDTO.getId());
        dishFlavorService.remove(queryWrapper);
        //保存菜品口味数据
        dishDTO.getFlavors().forEach(flavor -> flavor.setDishId(dishDTO.getId()));
        dishFlavorService.saveOrUpdateBatch(dishDTO.getFlavors());
        return;
    }


}
